package backend.search.manager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import core.cms.model.Block;
import core.util.HibernateCoreUtil;

public class BlockSearchManager  extends HibernateCoreUtil
{
	public List<Block> getSimpleSearchResult(String searchkey)
	{
		List<Block> lstBlock = new ArrayList<Block>();
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Block block = null ;
		try 
		{
				transaction = session.beginTransaction();
				//from Category c where c.name='"+name+"' and c.path='"+path+"'")
				Iterator<Block> blockItr = session.createQuery("from Block psi where psi.identifier like '%"+searchkey+"%'").list().iterator();
				while(blockItr.hasNext())
				{
					block = blockItr.next() ;
					lstBlock.add(block);
				}
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return lstBlock;
	}
	
}
